﻿// --------------------------------
// <copyright file="ProductService.cs" company="OpenTicket">
//     Microsoft Public License (Ms-PL)
// </copyright>
// <author>Glenn</author>
// ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTicket.Model;
using OpenTicket.Repositories;
using OpenTicket.Repositories;
using System.Web.DomainServices;
using System.Web.Ria;

namespace OpenTicket.Services
{
    /// <summary>
    /// Servicelayer for product
    /// </summary>
    [EnableClientAccess]
    public class ProductService :DomainService, OpenTicket.Services.IProductService
    {
        /// <summary>
        /// The productRepository to use
        /// </summary>
        private IProductRepository productRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProductService"/> class.
        /// </summary>
        /// <param name="productRepository">The product repository.</param>
        public ProductService(IProductRepository productRepository)
        {
            this.productRepository = productRepository;
        }

        public ProductService()
        {
            this.productRepository = new StaticTestProductRepository();
        }

        /// <summary>
        /// Saves the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <returns>The Saved product</returns>
        public Model.Product Save(Model.Product product)
        {
            return this.productRepository.Save(product);
        }

        /// <summary>
        /// Gets the product by product code.
        /// </summary>
        /// <param name="productCode">The product code.</param>
        /// <returns>The Product</returns>
        public Model.Product GetProductByProductCode(string productCode)
        {
            return this.productRepository.GetAll().WithId(productCode).FirstOrDefault();   
        }

        /// <summary>
        /// Deletes the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        public void Delete(Product product)
        {
            this.productRepository.DeleteProduct(product);
        }

        #region IProductService Members


        public IEnumerable<Product> GetProducts()
        {
            return this.productRepository.GetAll().ToList();
        }

        #endregion
    }
}
